package com.huifer.wgx.service.impl;

import com.huifer.wgx.utlis.DateUtil;
import com.huifer.wgx.model.PersonnelEntity;
import com.huifer.wgx.model.TaskEntity;
import com.huifer.wgx.repository.PersonnelRepository;
import com.huifer.wgx.repository.TaskRepository;
import com.huifer.wgx.service.TaskService;
import com.huifer.wgx.wgxenum.TaskState;
import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.swing.Renderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * <p>Title : TaskServiceImpl </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-19
 */
@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskRepository taskRepository;
    @Autowired
    private PersonnelRepository personnelRepository;


    /**
     * 创建任务
     *
     * @param title 任务标题
     * @param note 任务内容
     * @param assignName 发放任务人员的名称
     * @return 任务
     */
    @Override
    public TaskEntity saveTask(String title, String note, String assignName) {

        try {
            PersonnelEntity assignPer = personnelRepository.findByPersonnelName(assignName);
            Timestamp startTime = DateUtil.getNowDate();
            List<TaskEntity> edits = taskRepository
                    .findByTaskAssignIdAndTaskAssignTime(assignPer.getPersonnelId(), startTime);

            if (assignPer != null && edits.size() == 0) {

                TaskEntity task = new TaskEntity();
                task.setTaskTitle(title);
                task.setTaskNote(note);
                task.setTaskAssignId(assignPer.getPersonnelId());
                task.setTaskIsok(TaskState.UNFINISHED.getDesc());

                task.setTaskAssignTime(DateUtil.getNowDate());
                TaskEntity save = taskRepository.save(task);
                return save;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 接受任务
     *
     * @param assignName 发放任务人员的名称
     * @param startTime 任务开始时间
     * @param doName 接收任务人员的名称
     * @return 任务
     */
    @Override
    public TaskEntity updateDoTaks(String assignName, Timestamp startTime, String doName) {

        try {
            PersonnelEntity assignPer = personnelRepository.findByPersonnelName(assignName);
            PersonnelEntity doTask = personnelRepository.findByPersonnelName(doName);

            if (assignPer != null && doTask != null) {
                List<TaskEntity> edits = taskRepository
                        .findByTaskAssignIdAndTaskAssignTime(assignPer.getPersonnelId(), startTime);
                TaskEntity edit = edits.get(edits.size() - 1);
                edit.setTaskIsok(TaskState.PROCESSING.getDesc());
                edit.setTaskDoId(doTask.getPersonnelId());
                TaskEntity save = taskRepository.save(edit);
                return save;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 完成任务
     *
     * @param assignName 发放任务人员的名称
     * @param startTime 任务开始时间
     * @param doName 接收任务人员的名称
     * @param endTime 完成时间
     * @return 任务
     */
    @Override
    public TaskEntity updateEndTask(String assignName, Timestamp startTime, String doName,
            Timestamp endTime) {
        try {

            PersonnelEntity assignPer = personnelRepository.findByPersonnelName(assignName);
            PersonnelEntity doTask = personnelRepository.findByPersonnelName(doName);

            Specification<TaskEntity> spec = (Specification<TaskEntity>) (root, criteriaQuery, criteriaBuilder) -> {
                Predicate p1 = criteriaBuilder
                        .equal(root.get("taskAssignId"), assignPer.getPersonnelId());
                Predicate p2 = criteriaBuilder
                        .equal(root.get("taskDoId"), doTask.getPersonnelId());
                Predicate p3 = criteriaBuilder
                        .equal(root.get("taskAssignTime"), startTime);

                return criteriaBuilder.and(p1, p2, p3);
            };

            TaskEntity endTask = taskRepository.findOne(spec).get();
            if (endTask != null) {

                endTask.setTaskIsok(TaskState.COMPLETE.getDesc());
                endTask.setTaskEndTime(endTime);
                TaskEntity save = taskRepository.save(endTask);
                return save;

            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询所有
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     */
    @Override
    public Page<TaskEntity> findAll(Integer pageNum, Integer pageSize) {
        Sort sort = new Sort(Direction.DESC, "taskId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        Page<TaskEntity> all = taskRepository.findAll(pageable);
        return all;
    }
}
